package com.cml.springcloud.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.cml.springcloud.service.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author cml
 * @create 2021/4/27 12:57
 */
@RestController
@Slf4j
public class FlowLimitController {

    @Resource
    private RestTemplate restTemplate;
    @Resource
    private Message message;

    String PAth ="http://nacos-provider-payment";

    @GetMapping("/testA")
    public String testA() {
//        return restTemplate.getForObject(PAth+"/payment/nacos/99",String.class)+"testA";
        return message.getMessage()+"testA";
    }

    @GetMapping("/testB")
    public String testB() {
//        return restTemplate.getForObject(PAth+"/payment/nacos/99",String.class)+"testB";
        return message.getMessage()+"testB";
    }

    @GetMapping("/testD")
    public String testD() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        return restTemplate.getForObject(PAth+"/payment/nacos/99",String.class)+"testB";
        System.out.println("***********testD**************");
//        int a=10/0;
        return "*******testD";
    }

    @GetMapping("/testhot")
    @SentinelResource(value = "testhot",blockHandler = "del_handler")
    public String testHotKey(@RequestParam(value = "p1",required = false) String p1,
                             @RequestParam(value = "p2",required = false) String p2){
        return "******testHotKey";
    }

    public String del_handler(String p1,String p2, BlockException blockException){
        return p1+"限制了";
    }
}
